package com.hanlink.dsp.ca.model.settings;

import androidx.annotation.NonNull;

import com.hanlink.dsp.DspContext;
import com.hanlink.dsp.DspSettings;
import com.hanlink.dsp.ca.model.channel.EqChannel;
import com.hanlink.dsp.model.BaseSettings;
import com.hanlink.dsp.protocols.DspProtocol;
import com.qztech.sdk.model.Grouping;
import com.qztech.sdk.model.SettingConstants;
import com.qztech.utils.YzDebug;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

@Table(name = "_eq_settings")
public class EqSettings extends BaseSettings {


    @Column(name = "eq_defeat")
    private int eqDefeat;// eq defeat 是否开启

    @Column(name = "effect")
    private int effect; // eq effect

    @Column(name = "base_ch1")
    private int baseCh1;
    @Column(name = "base_ch2")
    private int baseCh2;
    @Column(name = "base_ch3")
    private int baseCh3;
    @Column(name = "base_ch4")
    private int baseCh4;
    @Column(name = "base_ch5")
    private int baseCh5;
    @Column(name = "base_ch6")
    private int baseCh6;
    @Column(name = "base_ch7")
    private int baseCh7;
    @Column(name = "base_ch8")
    private int baseCh8;

    @Column(name = "last_channel")
    private int lastChannel = 0; //最后选中的channel 默认是0

    private final List<EqChannel> mEqChannels;// eqChannel
    public EqSettings() {
        super();
        this.mEqChannels = new ArrayList<>();
    }
    @Override
    public void resetData() {
        YzDebug.d("resetData: "+ mEqChannels.size() +" " + hashCode());
        setBaseChs(new int[]{0,0,0,0,0,0,0,0});
        eqDefeat = 0;
        effect = 0;
        lastChannel = 0;
        synchronized (mEqChannels){
            for (EqChannel channel : mEqChannels) {
                channel.resetData();
            }
        }

    }
    public void getChannels(DbManager db) throws DbException {

        List<EqChannel> eqChannels =  null;
        if (db != null){
            eqChannels= db.selector(EqChannel.class).where("settings_id", "=", this.id).orderBy("channel").findAll();
        }
        YzDebug.d("---getChannels:  "+(eqChannels == null?" empty":("size:"+eqChannels.size())) + " id:"+ this.id);
        if (eqChannels == null || eqChannels.size() <= 0) {
            eqChannels = new ArrayList<>();
            for (int i = 0; i < DspSettings.CHANNEL_LENGTH; i++) {
                eqChannels.add(new EqChannel(i, DspContext.getDefaultQValues(), DspContext.getDefaultGainValues(), DspProtocol.getDefaultChannelValues()));
            }
        }
        int index = 0;
        for (EqChannel channel : eqChannels) {
            YzDebug.d("---------getChannels:"+ index +" id:" + channel.getId() +" " + channel.getChannel());
            YzDebug.d("getChannels:"+ index +" " + channel.getFreqVal());
            YzDebug.d("getChannels:"+ index +" " + channel.getQVal());
            YzDebug.d("getChannels:"+ index +" " + channel.getGainVal());
            index++;
        }
        this.setEqChannels(eqChannels);
    }


    public void resetQValue(float resetValue) {
        for (EqChannel channel : mEqChannels) {
            channel.resetQValue(resetValue);
        }
    }


    public int getEqDefeat() {
        return eqDefeat;
    }

    public void setEqDefeat(int eqDefeat) {
        this.eqDefeat = eqDefeat;
    }

    public int getEffect() {
        return effect;
    }

    public void setEffect(int effect) {
        this.effect = effect;
    }

    public List<EqChannel> getEqChannels() {
        return mEqChannels;
    }

    public void setEqChannels(List<EqChannel> channels) {
        YzDebug.d("setEqChannels 1: "+ channels.size() +" " + hashCode(), new Throwable());
        synchronized (mEqChannels){
            mEqChannels.clear();
            mEqChannels.addAll(channels);
            for (EqChannel channel : mEqChannels) {
                channel.setSettingsId(this.id);
            }
        }
        YzDebug.d("setEqChannels 2: "+ channels.size());
    }

    public int getLastChannel() {
        return lastChannel;
    }

    public void setLastChannel(int lastChannel) {
        this.lastChannel = lastChannel;
    }

    public int getBaseCh1() {
        return baseCh1;
    }

    public void setBaseCh1(int baseCh1) {
        this.baseCh1 = baseCh1;
    }
    public void setBaseChs(int[] baseValues) {
        try {
            if (baseValues[0] != -1) baseCh1 = baseValues[0];
            if (baseValues[1] != -1) baseCh2 = baseValues[1];
            if (baseValues[2] != -1) baseCh3 = baseValues[2];
            if (baseValues[3] != -1) baseCh4 = baseValues[3];
            if (baseValues[4] != -1) baseCh5 = baseValues[4];
            if (baseValues[5] != -1) baseCh6 = baseValues[5];
            if (baseValues[6] != -1) baseCh7 = baseValues[6];
            if (baseValues[7] != -1) baseCh8 = baseValues[7];
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public int getBaseCh2() {
        return baseCh2;
    }

    public void setBaseCh2(int baseCh2) {
        this.baseCh2 = baseCh2;
    }

    public int getBaseCh3() {
        return baseCh3;
    }

    public void setBaseCh3(int baseCh3) {
        this.baseCh3 = baseCh3;
    }

    public int getBaseCh4() {
        return baseCh4;
    }

    public void setBaseCh4(int baseCh4) {
        this.baseCh4 = baseCh4;
    }

    public int getBaseCh5() {
        return baseCh5;
    }

    public void setBaseCh5(int baseCh5) {
        this.baseCh5 = baseCh5;
    }

    public int getBaseCh6() {
        return baseCh6;
    }

    public void setBaseCh6(int baseCh6) {
        this.baseCh6 = baseCh6;
    }

    public int getBaseCh7() {
        return baseCh7;
    }

    public void setBaseCh7(int baseCh7) {
        this.baseCh7 = baseCh7;
    }

    public int getBaseCh8() {
        return baseCh8;
    }

    public void setBaseCh8(int baseCh8) {
        this.baseCh8 = baseCh8;
    }

    public boolean isGroup12() {
        return baseCh1 == 1 || baseCh2 == 1;
    }
    public boolean isGrouped(){
        return  baseCh1 == 1 ||
                baseCh2 == 1 ||
                baseCh3 == 1 ||
                baseCh4 == 1 ||
                baseCh5 == 1 ||
                baseCh6 == 1 ||
                baseCh7 == 1 ||
                baseCh8 == 1  ;
    }
    public boolean isGroupAll() {
        return  baseCh1 == 1111 ||
                baseCh2 == 1111 ||
                baseCh3 == 1111 ||
                baseCh4 == 1111 ||
                baseCh5 == 1111 ||
                baseCh6 == 1111 ||
                baseCh7 == 1111 ||
                baseCh8 == 1111  ;
    }
    public int[] getGroupInfo(){
        return new int[]{
                baseCh1,
                baseCh2,
                baseCh3,
                baseCh4,
                baseCh5,
                baseCh6,
                baseCh7,
                baseCh8,
               };
    }

    public boolean isGroup34() {
        return baseCh3 == 1 || baseCh4 == 1;
    }


    public boolean isGroup56() {
        return baseCh5 == 1 || baseCh6 == 1;
    }


    public boolean isGroup78() {
        return baseCh7 == 1 || baseCh8 == 1;
    }

    public boolean isGroup1234() {
        return baseCh1 == 11 || baseCh2 == 11 || baseCh3 == 11 || baseCh4 == 11;
    }

    public int getGroup(int channel){
        switch (channel){
            case 0:
            case 1:
                if (isGroup12()) {
                    return 0;
                }
                break;
            case 2:
            case 3:
                if (isGroup34()) {
                    return 1;
                }
                break;
            case 4:
            case 5:
                if (isGroup56()) {
                    return 2;
                }
                break;
            case 6:
            case 7:
                if (isGroup78()) {
                    return 3;
                }
                break;
        }
        return channel;
    }

    @NonNull
    @Override
    public String toString() {
        return "EqSettings{" +
                "eqDefeat=" + eqDefeat +
                ", effect=" + effect +
                ", lastChannel=" + lastChannel +
                ", eqChannel=" + mEqChannels +
                '}';
    }

    public void reset() {
        effect = 0;
        if (isGroupAll()){
            resetChannel(Grouping.GROUP_ALL.getGroupIndexs());
        }else if (isGroup12()){
            resetChannel(Grouping.GROUP1_2.getGroupIndexs());
        }else if (isGroup34()){
            resetChannel(Grouping.GROUP3_4.getGroupIndexs());
        }else if (isGroup56()){
            resetChannel(Grouping.GROUP5_6.getGroupIndexs());
        }else if(isGroup78()){
            resetChannel(Grouping.GROUP7_8.getGroupIndexs());
        }else {
            resetChannel(new int[]{lastChannel});
        }
        setBaseChs(new int[]{0,0,0,0,0,0,0,0});
        DspProtocol.resetEq();
    }
    private void resetChannel(int[] index){
        if (index == null)return;
        synchronized (mEqChannels){
            for (int i : index) {
                mEqChannels.get(i).resetData();
            }
        }

    }


    public EqSettings copy(){
        final  EqSettings es = new EqSettings();
        es.id = id;
        es.refId = refId;
        es.eqDefeat = eqDefeat;
        es.effect = effect;
        es.baseCh1 = baseCh1;
        es.baseCh2 = baseCh2;
        es.baseCh3 = baseCh3;
        es.baseCh4 = baseCh4;
        es.baseCh5 = baseCh5;
        es.baseCh6 = baseCh6;
        es.baseCh7 = baseCh7;
        es.baseCh8 = baseCh8;
        es.lastChannel = lastChannel;
        for (EqChannel channel : mEqChannels) {
            es.mEqChannels.add(channel.copy());
        }
        return es;
    }

    public void copyIdFromSrc(EqSettings eqSettings) {
        this.id = eqSettings.id;
        this.refId = eqSettings.getRefId();
        for (EqChannel dst : mEqChannels) {
            for (EqChannel src : eqSettings.mEqChannels) {
                if (dst.getChannel() == src.getChannel()){
                    dst.setId(src.getId());
                    dst.setSettingsId(src.getSettingsId());
                }
            }
        }

    }
}
